package com.iniapp.app.web.action.admin;


import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.iniapp.app.web.action.BaseCtrl;
import com.inibb.app.po.SaI18NMsg;
import com.inibb.app.service.admin.II18nMsgService;
import com.inibb.app.vo.I18nMsgSC;

@Controller("i18nMsgCtrl")
@Scope("session")//page
public class I18nMsgCtrl extends BaseCtrl  implements Serializable{

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private II18nMsgService i18nMsgService;
	
	private LazyDataModel<SaI18NMsg> listModel;
	private SaI18NMsg selectedRecord;
	private SaI18NMsg editRecord = new SaI18NMsg();
	private LazyDataModel<SaI18NMsg> selectedRecords;

	@PostConstruct
	public void init(){
		I18nMsgSC paginationCriteria = new I18nMsgSC();
		paginationCriteria.setStartRow(0);
		paginationCriteria.setPageSize(10);
		listModel = searchPages(paginationCriteria);
		
	}

	private LazyDataModel<SaI18NMsg> searchPages(final I18nMsgSC paginationCriteria){
		LazyDataModel<SaI18NMsg> clm = new LazyDataModel<SaI18NMsg>() {
			private static final long serialVersionUID = 1L;

			@Override
			public List<SaI18NMsg> load(int rowStart, int pageSize, String arg2, SortOrder sorOder, Map<String, String> arg4) {
				paginationCriteria.setStartRow(rowStart);
				paginationCriteria.setPageSize(pageSize);
				
				return i18nMsgService.searchPages(paginationCriteria);
			}
			
			@Override  
		    public SaI18NMsg getRowData(String rowKey) {  
		        //In a real app, a more efficient way like a query by rowKey should be implemented to deal with huge data  
//				return i18nMsgService.getCode(Integer.parseInt(rowKey));
		        List<SaI18NMsg> cars = (List<SaI18NMsg>) this.getWrappedData();  
		        for(SaI18NMsg car : cars) {  
		            if(car.getId().toString().equals(rowKey))  
		                return car;  
		        }  
		          
		        return null;  
		    }  
		  
		    @Override  
		    public String getRowKey(SaI18NMsg car) {  
		        return car.getId().toString();
		    }  
		};
		int total = i18nMsgService.countPages(paginationCriteria);
		clm.setRowCount(total);
		return clm;
	}
	
	public void create(){
		editRecord = new SaI18NMsg();
	}
	
	public void edit(){
		editRecord = getSelectedRecord();
	}
	
	public void saveEdit(){
		i18nMsgService.save(editRecord);
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "保存记录成功！",  "保存记录成功！");  
        FacesContext.getCurrentInstance().addMessage("保存记录成功！", message);  
	}
	
	public void deleteSelected(){
		i18nMsgService.delete(selectedRecord.getId());
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "删除记录成功！",  "删除记录成功！");  
        FacesContext.getCurrentInstance().addMessage("删除记录成功！", message);  
	}

	public LazyDataModel<SaI18NMsg> getListModel() {
		return listModel;
	}

	public void setListModel(LazyDataModel<SaI18NMsg> listModel) {
		this.listModel = listModel;
	}

	public SaI18NMsg getSelectedRecord() {
		return selectedRecord;
	}

	public void setSelectedRecord(SaI18NMsg selectedRecord) {
		this.selectedRecord = selectedRecord;
	}

	public SaI18NMsg getEditRecord() {
		return editRecord;
	}

	public void setEditRecord(SaI18NMsg editRecord) {
		this.editRecord = editRecord;
	}

	public LazyDataModel<SaI18NMsg> getSelectedRecords() {
		return selectedRecords;
	}

	public void setSelectedRecords(LazyDataModel<SaI18NMsg> selectedRecords) {
		this.selectedRecords = selectedRecords;
	}
	
}
